package com.gshrms.setting.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gshrms.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.gshrms.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.gshrms.setting.domain.StaffInfoField;
import com.gshrms.setting.mapper.StaffInfoTableMapper;
import com.gshrms.setting.domain.StaffInfoTable;
import com.gshrms.setting.service.IStaffInfoTableService;

/**
 * 明细表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-13
 */
@Service
public class StaffInfoTableServiceImpl extends ServiceImpl<StaffInfoTableMapper,StaffInfoTable> implements IStaffInfoTableService
{
    @Autowired
    private StaffInfoTableMapper staffInfoTableMapper;

    /**
     * 查询明细表
     * 
     * @param tableId 明细表主键
     * @return 明细表
     */
    @Override
    public StaffInfoTable selectStaffInfoTableByTableId(Long tableId)
    {
        return staffInfoTableMapper.selectStaffInfoTableByTableId(tableId);
    }

    /**
     * 查询明细表列表
     * 
     * @param staffInfoTable 明细表
     * @return 明细表
     */
    @Override
    public List<StaffInfoTable> selectStaffInfoTableList(StaffInfoTable staffInfoTable)
    {
        return staffInfoTableMapper.selectStaffInfoTableList(staffInfoTable);
    }

    /**
     * 新增明细表
     * 
     * @param staffInfoTable 明细表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertStaffInfoTable(StaffInfoTable staffInfoTable)
    {
        staffInfoTable.setCreateTime(DateUtils.getNowDate());
        int rows = staffInfoTableMapper.insertStaffInfoTable(staffInfoTable);
        insertStaffInfoField(staffInfoTable);
        return rows;
    }

    /**
     * 修改明细表
     * 
     * @param staffInfoTable 明细表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateStaffInfoTable(StaffInfoTable staffInfoTable)
    {
        staffInfoTable.setUpdateTime(DateUtils.getNowDate());
        staffInfoTableMapper.deleteStaffInfoFieldByTableId(staffInfoTable.getTableId());
        insertStaffInfoField(staffInfoTable);
        return staffInfoTableMapper.updateStaffInfoTable(staffInfoTable);
    }

    /**
     * 批量删除明细表
     * 
     * @param tableIds 需要删除的明细表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStaffInfoTableByTableIds(Long[] tableIds)
    {
        staffInfoTableMapper.deleteStaffInfoFieldByTableIds(tableIds);
        return staffInfoTableMapper.deleteStaffInfoTableByTableIds(tableIds);
    }

    /**
     * 删除明细表信息
     * 
     * @param tableId 明细表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStaffInfoTableByTableId(Long tableId)
    {
        staffInfoTableMapper.deleteStaffInfoFieldByTableId(tableId);
        return staffInfoTableMapper.deleteStaffInfoTableByTableId(tableId);
    }

    /**
     * 新增自定义字段信息
     * 
     * @param staffInfoTable 明细表对象
     */
    public void insertStaffInfoField(StaffInfoTable staffInfoTable)
    {
        List<StaffInfoField> staffInfoFieldList = staffInfoTable.getStaffInfoFieldList();
        Long tableId = staffInfoTable.getTableId();
        if (StringUtils.isNotNull(staffInfoFieldList))
        {
            List<StaffInfoField> list = new ArrayList<StaffInfoField>();
            for (StaffInfoField staffInfoField : staffInfoFieldList)
            {
                staffInfoField.setTableId(tableId);
                list.add(staffInfoField);
            }
            if (list.size() > 0)
            {
                staffInfoTableMapper.batchStaffInfoField(list);
            }
        }
    }
}
